package edu.example.order.servlet;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

@WebServlet("/login")
public class loginServlet extends HttpServlet {
    private static final long serialVersionUID=1L;
//
//    public loginServlet(){
//        super();
//    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stucode=req.getParameter("stucode");
        String password=req.getParameter("pwd");
        String remember=req.getParameter("remember");
        String role=req.getParameter("role");
        //String error=req.getParameter("error");
        if (stucode==null||stucode.trim().isEmpty()||password==null||password.trim().isEmpty()){
            resp.getWriter().println("<script>alert('用户名和密码不能为空')</script>");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
//            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/house_manage","root","xbXB9494");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/studenthousionmanager","root","1234");
            String sql="select * from user where stucode=? and password=?";
//            String sql1="select role from user";
//            preparedStatement=connection.prepareStatement(sql1);
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,stucode);
            preparedStatement.setString(2,password);
            //preparedStatement.setString(3,role);
            rs=preparedStatement.executeQuery();
            if (rs.next()){
                role=rs.getString("role");
                HttpSession session=req.getSession(false);
                if (session == null) {
                    session = req.getSession();
                }
                session.setAttribute("stucode",stucode);
                session.setAttribute("role",role);

                req.setAttribute("success", "登录成功");
                req.getRequestDispatcher("main.jsp").forward(req,resp);
            }else{
                req.setAttribute("error", "用户名密码错误");
                req.getRequestDispatcher("login.jsp").forward(req, resp);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
                try {
                    if (rs!=null)
                    rs.close();
                    if (preparedStatement!=null)
                        preparedStatement.close();
                    if (connection!=null)
                        connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

        }
//        System.out.println("stucode:"+stucode+" pwd:"+password);
//        userService userService=new userServiceImpl();
//        User user =userService.findByNameAndPwd(stucode,password);
//        System.out.println("user"+user);
//        if (user==null){
//            req.setAttribute("error","输入学号或密码错误");
//            req.getRequestDispatcher("index.jsp").forward(req,resp);
//        }else {
//            System.out.println("======跳转到主页面======");
//            req.getRequestDispatcher("main.jsp").forward(req,resp);
//        }
    }

}
